/*
 * 
 * Version 1.00
 * 
 * Created on 2007-6-4
 * Encoding GB2312
 */
package net.comm.blog.service.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;
import java.util.Vector;

import net.comm.blog.bean.BaseBlog;
import net.comm.blog.db.BlogServiceSqlMysql;
import net.comm.blog.db.DbBlogService;
import net.comm.blog.web.form.BlogForm;
import net.comm.common.GlbFinal;
import net.comm.common.ReturnMessage;
import net.comm.common.common;
import net.comm.file.service.impl.FilesServiceImpl;
import net.comm.file.web.form.FilesForm;

import net.comm.message.service.impl.MessageServiceImpl;
import net.comm.message.web.form.MessageForm;
import net.comm.message.web.form.MsgcontForm;
import net.comm.oper.service.impl.OperLogServiceImpl;
import net.comm.oper.web.form.OperLogForm;
import net.comm.page.PageCtr;
import net.comm.param.service.impl.SequenceMgr;

import net.comm.tags.JstlFun;
import net.comm.user.service.impl.UserSysinfoServiceImpl;
import net.comm.user.web.form.UserSysinfoForm;
import net.comm.util.DatetimeUtil;
import net.comm.util.DbcpPool;
import net.comm.util.StringUtil;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.sakaiproject.db.impl.BasicSqlService;


/**
 *
 * @author hcgong(hcgong@sina.com)
 * @version 1.0
 * @date 2007-6-4
 */
public class BlogServiceImpl {

	static Logger logger = Logger.getLogger(BlogServiceImpl.class.getName());

	/**
	 * 
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#list(net.swift.module.other.ExaQueryBean, net.swift.common.ReturnMessage)
	 */
	public List list(BlogForm theForm,  ReturnMessage msg) 
	{
		List list = null;

		Connection cn = null;

		try
		{
			cn = DbcpPool.getConnection();
			
			BlogServiceSqlMysql servSql = new BlogServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();

			Object[] fields = new Object[1];
			
			fields[0] = theForm.getQryUserID();
			
			/*
			 * 获得查询SQL语句
			 */
			String sql = servSql.getCountSql2();

			/**
			 * 根据SQL执行结果计算分页相关信息
			 */
			PageCtr page = theForm.getPage();
			page.setTotalNum(bSqlServ.dbCount(cn, sql, fields));
			page.caculate();

			//取得FORM中传入的查询参数
			Object[] fields2 = new Object[3];
			
			fields2[0] = theForm.getQryUserID();
			fields2[1] = page.getPos()-1;
			fields2[2] = page.getPageSize();
			
			sql = servSql.getRecbyPageSql();

			list = bSqlServ.dbRead(cn, sql, fields2, new DbBlogService());
			
		    DbcpPool.returnConnection(cn);
		    
		} catch(Exception ex) 
		{
			/**
			 * 记录相关日志
			 */
			logger.error(ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
			
		} finally {
			DbcpPool.returnConnection(cn);
		}		
		
		return list;
	}

	/**
	 * 
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#list(net.swift.module.other.ExaQueryBean, net.swift.common.ReturnMessage)
	 */
	public List listallcomment(BlogForm theForm,  ReturnMessage msg) 
	{
		List list = null;

		Connection cn = null;

		try
		{
			cn = DbcpPool.getConnection();
			
			BlogServiceSqlMysql servSql = new BlogServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();

			Object[] fields = new Object[1];
			
			fields[0] = theForm.getQryBlogID();
			
			/*
			 * 获得查询SQL语句
			 */
			String sql = servSql.getCountSql3();

			/**
			 * 根据SQL执行结果计算分页相关信息
			 */
			PageCtr page = theForm.getPage();
			page.setTotalNum(bSqlServ.dbCount(cn, sql, fields));
			page.caculate();

			//取得FORM中传入的查询参数
			Object[] fields2 = new Object[3];
			
			fields2[0] = theForm.getQryBlogID();
			fields2[1] = page.getPos()-1;
			fields2[2] = page.getPageSize();
			
			sql = servSql.getblogcommentbyPageSql();

			list = bSqlServ.dbRead(cn, sql, fields2, new DbBlogService());
			
		    DbcpPool.returnConnection(cn);
		    
		} catch(Exception ex) 
		{
			/**
			 * 记录相关日志
			 */
			logger.error(ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
			
		} finally {
			DbcpPool.returnConnection(cn);
		}		
		
		return list;
	}

	/**
	 * 
	 */
	public List adlist(BlogForm theForm,  ReturnMessage msg) 
	{
		List list = new Vector();

		Connection cn = null;
		Statement stmt=null;
		ResultSet rs=null;
		try
		{
			cn = DbcpPool.getConnection();
			
			BlogServiceSqlMysql servSql = new BlogServiceSqlMysql();
			
			StringBuffer sb = new StringBuffer(" where 1=1 ");

			if(theForm.getQryBlogID() > 0){
				sb.append(" and BlogID=");
				sb.append(theForm.getQryBlogID());
				sb.append(" ");
			}

			if(theForm.getQryFollID() > 0){
				sb.append(" and FollID=");
				sb.append(theForm.getQryFollID());
				sb.append(" ");
			}
			
			if(theForm.getQryUpFollID() > 0){
				sb.append(" and UpFollID=");
				sb.append(theForm.getQryUpFollID());
				sb.append(" ");
			}
			
			if(theForm.getQryUserID() > 0 ){
				
				sb.append(" and  UserID =");
				sb.append(theForm.getQryUserID());
				sb.append(" ");
			}
			
			if(!theForm.getQryOrderby().equals("")){
				
				sb.append(" Order by ");
				sb.append(theForm.getQryOrderby());
				sb.append(" ");
			}else{

				sb.append(" Order by PubTime desc ");	
			}

			//查询记录数
			stmt = cn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
			String sql = servSql.getCountSql1();

			logger.debug(sql+sb.toString());
			rs=stmt.executeQuery(sql+sb.toString());
			int nRows = 0;
			if (rs.next())
	           nRows = rs.getInt("COUNT");
			else
			{
	            rs.close();
	            stmt.close();
	            DbcpPool.returnConnection(cn);
			    return list;
			}
			/**
			 * 根据SQL执行结果计算分页相关信息
			 */
			PageCtr page = theForm.getPage();
			page.setTotalNum(nRows);
			page.caculate();

			sb.append(" limit ");
			sb.append(page.getPos()-1);
			sb.append(", ");
			sb.append(page.getPageSize());
			
			 //取记录
			 sql = servSql.getRecSql();
			 logger.debug(sql+sb.toString());
			 DbBlogService dbblog = new DbBlogService();
			 
			 rs=stmt.executeQuery(sql+sb.toString());
			 while (rs.next())
			 {
				try
				{
					Object obj = dbblog.readSqlResultRecord(rs);
					if (obj != null) list.add(obj);
				}
				catch (Throwable t)
				{
					logger.error(t.getMessage());
				}		
			 }
			 rs.close();
			 stmt.close();
		     DbcpPool.returnConnection(cn);

		} catch(Exception ex) 
		{
			/**
			 * 记录相关日志
			 */
			logger.error(ex.getMessage());
			try {
				if (rs != null)
					rs.close();
				if (stmt != null)
					stmt.close();
				DbcpPool.returnConnection(cn);
			} catch (Exception ex1) {
				ex1.printStackTrace();
			}			
	    }finally {
			try {
				if (rs != null)
					rs.close();
				if (stmt != null)
					stmt.close();
			} catch (Exception ex) {
				ex.printStackTrace();
			}			
			DbcpPool.returnConnection(cn);
		}		
		return list;
	}		

	/**
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#get(net.swift.db.tab_paramnostro, net.swift.common.ReturnMessage)
	 */
	public void get(BlogForm theForm, ReturnMessage msg) 
	{
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			DbcpPool.beginTran(cn);
			
			List list = null;

			BlogServiceSqlMysql servSql = new BlogServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			//取得FORM中传入的查询参数
			Object[] fields2 = new Object[3];
			
			fields2[0] = theForm.getBlogID();
			fields2[1] = theForm.getFollID();
			fields2[2] = theForm.getUpFollID();
			
			String sql = servSql.getbykeySql();			
					
			list = bSqlServ.dbRead(cn, sql, fields2, new DbBlogService());
			
			if(list.size() == 0){
				common.setAppFailMsg(msg , " 取记录失败，请检查！");
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;				
			}
			
			BaseBlog base = (BaseBlog)list.get(0);
			
			BeanUtils.copyProperties(theForm, base); 
			
			if(theForm.getFollID() == 0 && theForm.getUpFollID() == 0){
				
				//更新点击数并取到日志信息
				BlogForm blogForm = new BlogForm();
				
				blogForm.setBlogID(theForm.getBlogID());
				blogForm.setAttachNum(1);
				
				updsts(cn,blogForm,msg);
				if(msg.getErrCode() != 0){
					return ;
				}				
			}
			
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);
			return;
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about info" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}
	

	/**
	 * 添加博客到表中
	 * @see net.IUserbasicService.module.other.IUserService#add(net.swift.common.ReturnMessage)
	 */
	public void add(BlogForm theForm, String path, ReturnMessage msg) 
	{
		
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			
			DbcpPool.beginTran(cn);
			
			BlogServiceSqlMysql servSql = new BlogServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			theForm.setBlogID(SequenceMgr.generateSeq( GlbFinal.SEQ_INITDATE,GlbFinal.SEQ_BLOGID));
			
			//保存附件一
			if(theForm.getFilepath() != null&&!theForm.getFilepath().getFileName().equals("")){
				
				FilesForm fileForm = new FilesForm();
				fileForm.setFormFile(theForm.getFilepath());
				fileForm.setF_info(theForm.getTitle());
				fileForm.setF_Zip(GlbFinal.ZIP_600);
				
				FilesServiceImpl fileservice = new FilesServiceImpl();
				
				fileservice.add(cn, fileForm, msg);
				if(msg.getErrCode() != 0){
					return;
				}
				theForm.setF_ID(fileForm.getF_ID());
			}				
	        String intro = StringUtil.htmlTotxt(theForm.getContent());
	        
	        int len = intro.length();
	        if(len > 254){
	        	len = 254;
	        }
	        theForm.setIntro(intro.substring(0,len));
			
			//取得FORM中传入的查询参数
			Object[] fields = new Object[20];
			
			fields[0] = theForm.getBlogID();		//博客ID
			fields[1] = theForm.getFollID();		//	评论ID
			fields[2] = theForm.getUpFollID();		//	评论ID
			fields[3] = theForm.getTitle();			//	博客标题
			fields[4] = theForm.getIntro();			//	内容简介
			fields[5] = theForm.getContent();		//	内容
			fields[6] = theForm.getTags();			//	关键词
			fields[7] = theForm.getF_ID();			//	附件ID
			fields[8] = DatetimeUtil.getDatetime();		//	发布时间
			fields[9] = theForm.getUserID();		//	用户ID
			fields[10] = theForm.getUpUserID();		//	用户ID
			fields[11] = theForm.getAttachNum();	//	浏览次数
			fields[12] = theForm.getReplyNum();		//	评论次数
			fields[13] = theForm.getFirsort();		//	分类一
			fields[14] = theForm.getSecsort();		//	分类二
			fields[15] = theForm.getThisort();		//	分类三
			fields[16] = theForm.getReplyFlg();		//	是否允许评论
			fields[17] = theForm.getBlogSet();		//	
			fields[18] = theForm.getStatus();		//	状态
			fields[19] = DatetimeUtil.getDatetime();//	最后修改日期			
			
			String sql = servSql.getInsertSql();
						
			boolean  res = bSqlServ.dbWrite(cn, sql, fields);
			if(!res){
				common.setAppFailMsg(msg , " 添加日志失败！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return ;
			}
			//更新博客数量
			UserSysinfoServiceImpl syservice  = new UserSysinfoServiceImpl();
			UserSysinfoForm sysForm = new UserSysinfoForm();

			sysForm.setUserID(theForm.getUserID());

			sysForm.setBlogNum(1);
			syservice.update(cn, sysForm, msg);
			if(msg.getErrCode() != 0){
				return;
			}	
			
			//增加动态日志
			OperLogServiceImpl logservice = new OperLogServiceImpl();
			OperLogForm logForm = new OperLogForm();
			
			logForm.setUserID(theForm.getUserID());
			logForm.setType(GlbFinal.OPERTYPE_BLOG);					//日志
			
			String[] arr = new String[7];
			arr[0] = JstlFun.GetUserNick(theForm.getUserID());			//主操作者		
			arr[1] = "";												//主动者与被动者的关系副词
			arr[2] = "";												//被动接受者ID
			arr[3] = "";												//被动接受者姓名
			arr[4] = "发表了新日志：";									//操作
			arr[5] = theForm.getTitle();								//操作对象名称
			arr[6] = String.valueOf(theForm.getBlogID());				//操作对象KEY，多个字段以'|'分隔

			logForm.setLogarr(arr);
			
			logservice.add(cn, logForm, msg);
			if(msg.getErrCode() != 0){
				return;
			}				
			
			
			
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);

		} catch(Exception ex) 
		{
			common.setAppFailMsg(msg , ex.getMessage());
			logger.error("System error , param get about info" + ex);
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}
	/**
	 * 添加项目博客评论到表中
	 * @see net.IUserbasicService.module.other.IUserService#add(net.swift.common.ReturnMessage)
	 */
	public void addcomment(BlogForm theForm, String path, ReturnMessage msg) 
	{
		
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			
			DbcpPool.beginTran(cn);
			
			BlogServiceSqlMysql servSql = new BlogServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			//保存附件一
			if(theForm.getFilepath() != null&&!theForm.getFilepath().getFileName().equals("")){
				
				FilesForm fileForm = new FilesForm();
				fileForm.setFormFile(theForm.getFilepath());
				fileForm.setF_info(theForm.getTitle());
				fileForm.setF_Zip(GlbFinal.ZIP_600);
				
				FilesServiceImpl fileservice = new FilesServiceImpl();
				
				fileservice.add(cn, fileForm, msg);
				if(msg.getErrCode() != 0){
					return;
				}
				theForm.setF_ID(fileForm.getF_ID());
			}				
			
			//取得跟帖编号
			List list = null;
			String sql = "";
			//
			if(theForm.getFollID() > 0){
				//取跟帖序号
				Object[] fields2 = new Object[2];
				fields2[0] = theForm.getBlogID();
				fields2[1] = theForm.getFollID();
				
				sql = servSql.getMaxUpfollIDRecSql();			
						
				list = bSqlServ.dbRead(cn, sql, fields2, new DbBlogService());
				if(list.size() ==0){
					common.setAppFailMsg(msg , " 取记录失败，请检查！");
					DbcpPool.closeTran(cn);
					DbcpPool.returnConnection(cn);
					return;				
				}
				BaseBlog base = (BaseBlog)list.get(0);	
				
				theForm.setUpFollID(base.getUpFollID() + 1);
				
			}else{
				Object[] fields1 = new Object[1];
				fields1[0] = theForm.getBlogID();
				
				sql = servSql.getMaxfollIDRecSql();			
						
				list = bSqlServ.dbRead(cn, sql, fields1, new DbBlogService());
				if(list.size() ==0){
					common.setAppFailMsg(msg , " 取记录失败，请检查！");
					DbcpPool.closeTran(cn);
					DbcpPool.returnConnection(cn);
					return;				
				}
				BaseBlog base = (BaseBlog)list.get(0);	
				
				theForm.setFollID(base.getFollID() + 1);
			}
			
			//取得FORM中传入的查询参数
			Object[] fields = new Object[20];
			
			fields[0] = theForm.getBlogID();
			fields[1] = theForm.getFollID();
			fields[2] = theForm.getUpFollID();
			fields[3] = theForm.getTitle();
			fields[4] = theForm.getIntro();
			fields[5] = theForm.getContent();
			fields[6] = theForm.getTags();
			fields[7] = theForm.getF_ID();
			fields[8] = DatetimeUtil.getDatetime();
			fields[9] = theForm.getUserID();
			fields[10] = theForm.getUpUserID();
			fields[11] = theForm.getAttachNum();
			fields[12] = theForm.getReplyNum();
			fields[13] = theForm.getFirsort();
			fields[14] = theForm.getSecsort();
			fields[15] = theForm.getThisort();
			fields[16] = theForm.getReplyFlg();
			fields[17] = theForm.getBlogSet();		//	
			fields[18] =  theForm.getStatus();
			fields[19] = DatetimeUtil.getDatetime();
			
			sql = servSql.getInsertSql();
						
			boolean  res = bSqlServ.dbWrite(cn, sql, fields);
			if(!res){
				common.setAppFailMsg(msg , " 添加评论失败！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return ;
			}
			//更新评论数并取到日志信息
			BlogForm blogForm = new BlogForm();
			
			blogForm.setBlogID(theForm.getBlogID());
			blogForm.setReplyNum(1);
			
			updsts(cn,blogForm,msg);
			if(msg.getErrCode() != 0){
				return;
			}
			
			//更新用户评论数
			UserSysinfoServiceImpl syservice  = new UserSysinfoServiceImpl();
			UserSysinfoForm sysForm = new UserSysinfoForm();
			if(theForm.getFollID() > 0){
				sysForm.setUserID(theForm.getUpUserID());
			}else{
				sysForm.setUserID(theForm.getUserID());
			}

			sysForm.setPinlunNum(1);
			syservice.update(cn, sysForm, msg);
			if(msg.getErrCode() != 0){
				return;
			}	
//			//发送邮件到日志发表会员
//			MessageServiceImpl msgservice = new MessageServiceImpl();
//			MessageForm msgForm = new MessageForm(); 
//			MsgcontForm msgcontForm = new MsgcontForm();
//			
//			msgForm.setReceiver(blogForm.getUserID());
//			msgForm.setSender(theForm.getUserID());
//			msgForm.setMtype(GlbFinal.MTYPE_PINGLUN);
//			msgForm.setTitle(blogForm.getTitle()+" 新评论");
//			
//			msgcontForm.setMsg("<a href='/users.do?action=home&userID=" + msgForm.getSender() + "' target='_blank'>" +
//					JstlFun.GetUserNick(msgForm.getSender()) +
//					"</a>评论了<a href='/blog.do?action=info&qryBlogID="
//					+blogForm.getBlogID()+"' target='_blank'>"+ blogForm.getTitle() + "</a>");
//			
//			msgservice.add(cn, msgForm, msgcontForm, msg);
//			if(msg.getErrCode() != 0){
//				
//				return;
//			}
			
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);

		} catch(Exception ex) 
		{
			common.setAppFailMsg(msg , ex.getMessage());
			logger.error("System error , param get about info" + ex);
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}
	/**
	 * 删除一条指定博客
	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public void delete(BlogForm theForm,String path, ReturnMessage msg) 
	{
		
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			
			DbcpPool.beginTran(cn);
			
			List list = null;

			//取得FORM中传入的查询参数
			Object[] fields2 = new Object[3];
			
			fields2[0] = theForm.getBlogID();
			fields2[1] = theForm.getFollID();
			fields2[2] = theForm.getUpFollID();
			
			
			BlogServiceSqlMysql servSql = new BlogServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			String sql = servSql.getbykeySql();			
					
			list = bSqlServ.dbRead(cn, sql, fields2, new DbBlogService());
			
			if(list.size() ==0){
				common.setAppFailMsg(msg , " 取记录失败，请检查！");
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;				
			}
			
			BaseBlog base = (BaseBlog)list.get(0);
			if(base.getF_ID() > 0){
				
				FilesServiceImpl fileservice = new FilesServiceImpl();
				FilesForm fileForm = new FilesForm();
				fileForm.setF_ID(base.getF_ID());
				
				
				fileservice.delete(cn, fileForm, msg);
				if(msg.getErrCode() != 0){
					return;
				}
	       	}
			sql = servSql.getDeleteSql();
			
			boolean  res = bSqlServ.dbWrite(cn, sql, fields2);
			if(!res){
				common.setAppFailMsg(msg , " 删除记录失败，请检查！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;
			}
			
			//更新日志评论数
			BlogForm blogForm = new BlogForm();
			
			blogForm.setBlogID(theForm.getBlogID());
			blogForm.setReplyNum(-1);
			
			updsts(cn,blogForm,msg);
			if(msg.getErrCode() != 0){
				return;
			}			
			
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);
			return;
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about info" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}

	/**
	 * 删除一条博客及所有回复信息
	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public void delall(BlogForm theForm,String path, ReturnMessage msg) 
	{
		
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			
			DbcpPool.beginTran(cn);
			
			BlogServiceSqlMysql servSql = new BlogServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			List list = null;

			//取得博客及所有回复信息
			Object[] fields2 = new Object[1];
			
			fields2[0] = theForm.getBlogID();
			
			String sql = servSql.getbyblogIDSql();			
					
			list = bSqlServ.dbRead(cn, sql, fields2, new DbBlogService());
			if(list.size() == 0){
				common.setAppFailMsg(msg , " 取日志失败，请检查！");
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;				
			}
			BaseBlog base = (BaseBlog)list.get(0);
			BeanUtils.copyProperties(theForm, base); 
			
			int len = list.size();
			FilesServiceImpl fileservice = new FilesServiceImpl();
			FilesForm fileForm = new FilesForm();
			boolean  res = true;
			Object[] fields = new Object[3];
			sql = servSql.getDeleteSql();
			for(int i=0; i< len; i++){
				
				base = (BaseBlog)list.get(i);
				if(base.getF_ID() > 0){
					fileForm.Clear();
					fileForm.setF_ID(base.getF_ID());
					fileservice.delete(cn, fileForm, msg);
					if(msg.getErrCode() != 0){
						return;
					}
		       	}
				fields[0] = base.getBlogID();
				fields[1] = base.getFollID();
				fields[2] = base.getUpFollID();
				
				res = bSqlServ.dbWrite(cn, sql, fields);
				if(!res){
					common.setAppFailMsg(msg , " 删除记录失败，请检查！");
					DbcpPool.rollBackTran(cn);
					DbcpPool.closeTran(cn);
					DbcpPool.returnConnection(cn);
					return;
				}				
			}
			
			//更新用户的日志数量数量
			UserSysinfoServiceImpl syservice  = new UserSysinfoServiceImpl();
			UserSysinfoForm sysForm = new UserSysinfoForm();

			sysForm.setUserID(theForm.getUserID());

			sysForm.setBlogNum(-1);
			syservice.update(cn, sysForm, msg);
			if(msg.getErrCode() != 0){
				return;
			}					
			
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);
			return;
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about info" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}

	/**
	 * 更新一条指定博客
	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public void update(BlogForm theForm,String path, ReturnMessage msg) 
	{
		
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			DbcpPool.beginTran(cn);
	
			BlogServiceSqlMysql servSql = new BlogServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			//取得FORM中传入的查询参数
			Object[] fields2 = new Object[3];
			
			fields2[0] = theForm.getBlogID();
			fields2[1] = theForm.getFollID();
			fields2[2] = theForm.getUpFollID();
			
			String sql = servSql.getbykeySql();			
					
			List list = bSqlServ.dbRead(cn, sql, fields2, new DbBlogService());
			if(list.size() ==0){
				common.setAppFailMsg(msg , " 取记录失败，请检查！");
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;				
			}
			
			BaseBlog base = (BaseBlog)list.get(0);
		
			//保存附件一
			if(theForm.getFilepath() != null&&!theForm.getFilepath().getFileName().equals("")){
				
				FilesForm fileForm = new FilesForm();
				fileForm.setFormFile(theForm.getFilepath());
				fileForm.setF_info(theForm.getTitle());
				fileForm.setF_Zip(GlbFinal.ZIP_600);
				fileForm.setF_ID(base.getF_ID());
				
				FilesServiceImpl fileservice = new FilesServiceImpl();
				
				fileservice.update(cn, fileForm, msg);
				if(msg.getErrCode() != 0){
					return;
				}
				theForm.setF_ID(fileForm.getF_ID());
			}				

			//取得FORM中传入的查询参数
			Object[] fields = new Object[20];
			
			fields[0] = theForm.getTitle();			//	博客标题
			fields[1] = theForm.getIntro();			//	内容简介
			fields[2] = theForm.getContent();		//	内容
			fields[3] = theForm.getTags();			//	关键词
			fields[4] = theForm.getF_ID();			//	附件ID
			fields[5] = theForm.getPubTime();		//	发布时间
			fields[6] = theForm.getUserID();		//	用户ID
			fields[7] = theForm.getUpUserID();		//	用户ID
			fields[8] = theForm.getAttachNum();		//	浏览次数
			fields[9] = theForm.getReplyNum();		//	评论次数
			fields[10] = theForm.getFirsort();		//	分类一
			fields[11] = theForm.getSecsort();		//	分类二
			fields[12] = theForm.getThisort();		//	分类三
			fields[13] = theForm.getReplyFlg();		//	是否允许评论
			fields[14] = theForm.getBlogSet();		//	
			fields[15] = theForm.getStatus();		//	状态
			fields[16] = DatetimeUtil.getDatetime();//	最后修改日期	
			fields[17] = theForm.getBlogID();		//博客ID
			fields[18] = theForm.getFollID();		//	评论ID
			fields[19] = theForm.getUpFollID();		//	评论ID
			
			sql = servSql.getUpdateSql();
						
			boolean  res = bSqlServ.dbWrite(cn, sql, fields);
			if(!res){
				common.setAppFailMsg(msg , " 更新项目记录失败！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return ;
			}
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);

		} catch(Exception ex) 
		{
			common.setAppFailMsg(msg , ex.getMessage());
			logger.error("System error , param get about info" + ex);
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}
	/**
	 * 更新一条指定博客
	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public void updsts(BlogForm theForm, ReturnMessage msg) 
	{
		
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			DbcpPool.beginTran(cn);
	
			updsts( cn,  theForm, msg);
			if(msg.getErrCode() != 0){
				return;
			}
			
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);

		} catch(Exception ex) 
		{
			common.setAppFailMsg(msg , ex.getMessage());
			logger.error("System error , param get about info" + ex);
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}
	/**
	 * 更新一条指定博客
	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public void updsts(Connection cn, BlogForm theForm, ReturnMessage msg) 
	{
		
		try
		{
	
			BlogServiceSqlMysql servSql = new BlogServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			//读取原日志信息
			Object[] fields3 = new Object[3];
			fields3[0] = theForm.getBlogID();
			fields3[1] = 0;
			fields3[2] = 0;
			
			String sql = servSql.getbykeySql();			
			
			List list = bSqlServ.dbRead(cn, sql, fields3, new DbBlogService());
			if(list.size() ==0){
				common.setAppFailMsg(msg , " 取记录失败，请检查！");
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;				
			}
			BaseBlog base = (BaseBlog)list.get(0);	
		
			if(theForm.getBlogSet().equals("")){
				theForm.setBlogSet(base.getBlogSet());
			}
			if(theForm.getStatus().equals("")){
				theForm.setStatus(base.getStatus());
			}

			//更新日志评论数
			Object[] fields6 = new Object[7];
			
			fields6[0] = base.getAttachNum() + theForm.getAttachNum();
			fields6[1] = base.getReplyNum() + theForm.getReplyNum();
			fields6[2] = theForm.getBlogSet();
			fields6[3] = theForm.getStatus();
			fields6[4] = theForm.getBlogID();
			fields6[5] = 0;
			fields6[6] = 0;
			
			sql = servSql.getUpdAttRepSql();
						
			boolean res = bSqlServ.dbWrite(cn, sql, fields6);
			if(!res){
				common.setAppFailMsg(msg , " 更新评论数失败！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return ;
			}	
			
			BeanUtils.copyProperties(theForm, base); 
			

		} catch(Exception ex) 
		{
			common.setAppFailMsg(msg , ex.getMessage());
			logger.error("System error , param get about info" + ex);
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {

		}
		
		return ;
	}	
}
